<?php
/**
 * @brief 
 *
 * @license		http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License
 * @copyright	2008 David Singer
 * @author		David Singer <david@ramaboo.com>
 * @version		1.0.0
 */

require_once('../../includes/common.php');

try {
	// check requried params
	if (!isset($_POST['zone'])) {
		throw new Exception("Zone is required.");
	}
	$zone = htmlentities(strtolower(trim($_POST['zone'])));
	
	if (!isset($_POST['type'])) {
		throw new Exception("Type is required.");
	}
	$type = htmlentities(strtoupper(trim($_POST['type'])));
	
	
	$z = new Zone;
	$z->open($zone);
	
	$records = $z->getRecordsByType($type);
	
	$rows = array();
	// build rows
	foreach($records as $record) {
		$tmp = array(); // clear
		$tmp['id'] = $record->getId();
		$tmp['host'] = $record->getHost();
		$tmp['data'] = $record->getData();
		$tmp['ttl'] = $record->getTTL();
		$tmp['priority'] = $record->getMXPriority();
		$rows[] = $tmp;
	}
	
	$json['records'] = $rows;
	$json['success'] = true;
	$json['msg'] = "Zone $zone $type records opened.";
} catch (Exception $e) {
	$json['success'] = false;
	$json['msg'] = $e->getMessage();
}
$output = json_encode($json);
header_no_cache(strlen($output), 'application/json');
echo $output;
?>